package com.angel.service.ServiceImpl;

import com.angel.bean.Prescription;
import com.angel.bean.dto.IllCaseDto;
import com.angel.mapper.PrescriptionDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import com.angel.bean.IllCase;
import com.angel.mapper.IllCaseDao;
import com.angel.service.IllCaseService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class IllCaseServiceImpl implements IllCaseService {

    @Resource
    private IllCaseDao illCaseDao;

    @Resource
    private PrescriptionDao prescriptionDao;

    @Override
    public int insert(IllCase illCase) {
        return illCaseDao.insert(illCase);
    }

    @Override
    public int insertSelective(IllCase illCase) {
        return illCaseDao.insertSelective(illCase);
    }

    @Override
    public int insertList(List<IllCase> illCases) {
        return illCaseDao.insertList(illCases);
    }

    @Override
    public int update(IllCase illCase) {
        return illCaseDao.update(illCase);
    }

    @Override
    public List<IllCase> findIllCase(IllCase illCase) {
        return illCaseDao.findIllCase(illCase);
    }

    @Override
    public IllCase findOne(IllCase illCase) {
        List<IllCase> list = this.findIllCase(illCase);
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<IllCaseDto> getIllCaseAndBabyInfoAndUserInfo(IllCaseDto illCaseDto) {
        return illCaseDao.getIllCaseAndBabyInfoAndUserInfo(illCaseDto);
    }

    @Override
    public int delete(IllCase illCase) {
        return illCaseDao.delete(illCase);
    }

    @Override
    public int getCountAndBabyid(IllCase illCase) {
        return illCaseDao.getCountAndBabyid(illCase);
    }

    @Override
    public int getIllCaseState5Count(IllCase illCase) {
        return illCaseDao.getIllCaseState5Count(illCase);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int reCheck(Integer illids) {
        Prescription prescription = new Prescription();
        prescription.setIllCaseId(illids);
        int re = prescriptionDao.delPrescription(prescription);
        if (re != 0) {
            IllCase illCase = new IllCase();
            illCase.setIllCaseId(illids);
            illCase.setIllCaseState(1);
            int res = illCaseDao.update(illCase);
            if (res != 0) {
                return 1;
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        return 0;
    }
}
